package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.CanCookMapper;
import com.srs.backend.mapper.ChefMapper;
import com.srs.backend.mapper.FoodMapper;
import com.srs.backend.pojo.CanCook;
import com.srs.backend.pojo.Chef;
import com.srs.backend.pojo.Food;
import com.srs.backend.service.CanCookService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class CanCookServiceImpl implements CanCookService {
    @Resource
    private CanCookMapper canCookMapper;
    @Resource
    private ChefMapper chefMapper;
    @Resource
    private FoodMapper foodMapper;

    // 添加单一对应关系
    @Override
    public Result add(Map<String, String> data) {
        String chefIdStr = data.get("chef_id");
        String foodIdStr = data.get("food_id");
        Integer chefId, foodId;
        try {
            chefId = Integer.parseInt(chefIdStr);
            foodId = Integer.parseInt(foodIdStr);
        } catch (Exception e) {
            return Result.error("数据类型有误");
        }

        Chef chef = chefMapper.selectById(chefId);
        Food food = foodMapper.selectById(foodId);

        if (chef == null) {
            return Result.error("厨师不存在会已被删除");
        }
        if (food == null) {
            return Result.error("菜品不存在会已被删除");
        }

        QueryWrapper<CanCook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chef_id", chefId);
        queryWrapper.eq("food_id", foodId);
        CanCook canCook = canCookMapper.selectOne(queryWrapper);

        if (canCook != null) {
            return Result.error("该记录已存在，请勿重复添加");
        }

        canCookMapper.insert(new CanCook(null, chefId, foodId));

        return Result.ok();
    }

    // 删除单一对应关系
    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        CanCook canCook = canCookMapper.selectById(id);
        if (canCook == null) {
            return Result.error("会做的菜的记录不存在会已被删除");
        }
        return Result.ok();
    }

    // 修改单一对应关系
    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        CanCook canCook = canCookMapper.selectById(id);

        String chefIdStr = data.get("chef_id");
        String foodIdStr = data.get("food_id");
        Integer chefId, foodId;
        try {
            chefId = Integer.parseInt(chefIdStr);
            foodId = Integer.parseInt(foodIdStr);
        } catch (Exception e) {
            return Result.error("数据类型有误");
        }

        Chef chef = chefMapper.selectById(chefId);
        Food food = foodMapper.selectById(foodId);

        if (chef == null) {
            return Result.error("厨师不存在会已被删除");
        }
        if (food == null) {
            return Result.error("菜品不存在会已被删除");
        }

        canCook.setChefId(chefId);
        canCook.setFoodId(foodId);

        canCookMapper.updateById(canCook);

        return Result.ok();
    }

    @Override
    public Result getAll() {
        return Result.ok(canCookMapper.selectList(null));
    }
    // 获取厨师所有会做的菜
    @Override
    public Result getAllByChefId(Map<String, String> data) {
        String chefIdStr = data.get("chef_id");
        Integer chefId;
        try {
            chefId = Integer.parseInt(chefIdStr);
        } catch (Exception e) {
            return Result.error("数据类型有误");
        }

        Chef chef = chefMapper.selectById(chefId);

        if (chef == null) {
            return Result.error("厨师不存在会已被删除");
        }

        QueryWrapper<CanCook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chef_id", chefId);
        List<CanCook> canCooks = canCookMapper.selectList(queryWrapper);

        List<Food> foods = new LinkedList<>();

        for (int i = 0; i < canCooks.size(); i ++ ) {
            CanCook canCook = canCooks.get(i);
            foods.add(foodMapper.selectById(canCook.getFoodId()));
        }

        return Result.ok(foods);
    }
}
